<template>
  <!-- 论文发表 -->
  <div class="app-container">
    <searchForm
      :formOptions="formOptions"
      @onSearch="onSearch"
      @onReset="onReset"
      :formLabelWidth="formLabelWidth"
      v-show="showSearch"
    />
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
            v-hasPermi="['clr:pub:applyPaperPub']"
          >申请</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['clr:batch:edit']"
        >修改</el-button>
      </el-col>-->
      <!-- <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['clr:batch:remove']">删除</el-button>
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-table :data="blackList">
      <el-table-column
        label="学院"
        align="center"
        prop="collegeName"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column label="姓名" align="center" prop="clrName">
      </el-table-column>
      <el-table-column label="职称" align="center" prop="title">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.basic_title_type"
            :value="scope.row.title"
          />
        </template>
      </el-table-column>

      <el-table-column
        label="成果名称"
        align="center"
        prop="resultName" show-overflow-tooltip
      ></el-table-column>
      <el-table-column label="成果形式" align="center" prop="resultForm" show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.clr_result_form"
            :value="scope.row.resultForm"
          />
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="pubType" show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.clr_pub_type"
            :value="scope.row.pubType"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="发表时间"
        align="center"
        prop="pubDate"
        width="100" show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column label="发表网站" align="center" prop="pubWebsite" show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.clr_pub_website"
            :value="scope.row.pubWebsite"
          />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="sf" show-overflow-tooltip> </el-table-column>
      <el-table-column label="附件" align="center" prop="attachment">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleExport(scope.row)" v-if="scope.row.attachment"
            >下载附件</el-button
          >
          <el-button  size="mini" type="text" v-if="!scope.row.attachment"
            >暂无附件</el-button
          >
        </template> </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <!-- <el-button size="mini" type="text" @click="handleEdit(scope.row)"   v-hasPermi="['clr:pub:query']"
            >修改</el-button
          > -->
          <el-button size="mini" type="text" @click="handleDelete(scope.row)"   v-hasPermi="['clr:pub:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 申请 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="60%"
      append-to-body
      @close="cancel"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
         
          <el-col :span="12">
            <el-form-item label="成果名称" prop="resultName">
              <el-input
                v-model="form.resultName"
                placeholder="请输入成果名称"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="成果形式" prop="resultForm">
              <el-select
                v-model="form.resultForm"
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option
                  v-for="dict in dict.type.clr_result_form"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="论文类型" prop="pubType">
              <el-select
                v-model="form.pubType"
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option
                  v-for="dict in dict.type.clr_pub_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="发表时间" prop="pubDate">
              <el-date-picker
                v-model="form.pubDate"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="请选择时间"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发表网站" prop="pubWebsite">
              <el-select
                v-model="form.pubWebsite"
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option
                  v-for="dict in dict.type.clr_pub_website"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="certNumber">
              <el-input
                v-model="form.certNumber"
                placeholder="请输入备注"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="附件" prop="attachment">
              <uploud @input="changeLoud"></uploud>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
      <script>
import { collegeSelect } from "@/api/information/studentInfor";
import {downloadGet} from '@/utils/request'

import {
  pubList,
  applyPaperPub,
  pubUpdata,
  getPub,
  pubDel,
} from "@/api/clr/thesisList";
import uploud from "./components/uploud.vue";
import searchForm from "@/components/Search/searchForm.vue";
import { getDicts } from "@/api/system/dict/type";
import Validators from "@/utils/validate.js";
export default {
  name: "Dict",
  components: { searchForm,uploud },
  dicts: [
    "dep_grad_scope",
    "basic_title_type", // 职称类型
    "clr_result_form", // 成果形式
    "clr_pub_type", // 论文类型
    "clr_pub_website", // 发表网站
  ],

  data() {
    return {
      formLabelWidth: "100px",
      formOptions: [
        {
          label: "学院",
          prop: "deptId",
          element: "el-select",
          options: [],
          placeholder: "请选择"
        },
        {
          label: "姓名",
          prop: "clrName",
          element: "el-input",
          placeholder: "请输入姓名"
        },        
        {
          label: "职称",
          prop: "title",
          element: "el-select",
          options: [],
          placeholder: "请选择"
        },
        {
          label: "成果名称",
          prop: "resultName",
          element: "el-input",
          placeholder: "请输入成果名称"
        },
        {
          label: "成果形式",
          prop: "resultForm",
          element: "el-select",
          options: [],
          placeholder: "请选择"
        },
        {
          label: "论文类型",
          prop: "pubType",
          element: "el-select",
          options: [],
          placeholder: "请选择"
        },     
        {
          label: "发表时间",
          prop: "applyTime",
          type: "daterange",
          element: "el-date-picker",
          placeholder: "请选择时间"
        },      
        // {
        //   label: "发表时间",
        //   prop: "pubDate",
        //   type: "date",
        //   element: "el-date-picker",
        //   placeholder: "请选择时间"
        // },
        {
          label: "发表网站",
          prop: "pubWebsite",
          element: "el-select",
          options: [],
          placeholder: "请选择"
        },
      ],
      // 字典表格数据
      blackList: [],
      total: 0,
      form: {
        pubDate: "",
      },
      title: "",
      // 显示搜索条件
      showSearch: true,
      open: false, // 弹窗开关
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      dateRange:[],
      deptOptions: [], // 院系数据
      rules: {
        // collegeName: [
        //   { required: true, message: "请选择所属院系", trigger: "change" }
        // ],
        // clrName: [
        //   {
        //     required: true,
        //     text: "请输入姓名",
        //     validator: Validators.isString,
        //     trigger: "blur"
        //   }
        // ],
        // title: [
        //   { required: true, message: "请选择职称", trigger: "change" }
        // ],
        resultName: [
          {
            required: true,
            text: "请输入成果名称",
            validator: Validators.isStr,
            trigger: "blur"
          }
        ],
        resultForm: [
          { required: true, message: "请选择成果形式", trigger: "change" }
        ],
        pubType: [
          { required: true, message: "请选择论文类型", trigger: "change" }
        ],
       
        pubDate: [
          { required: true, message: "请选择发表时间", trigger: "change" }
        ],
        pubWebsite: [
          { required: true, message: "请选择发表网站", trigger: "change" }
        ],
      }
    };
  },
  created() {
    getDicts("basic_title_type").then(res => {
      this.formOptions[2].options = res.data.map(item => {
        return {
          value: item["dictValue"],
          label: item["dictLabel"]
        };
      });
    });
    getDicts("clr_result_form").then(res => {
      
      this.formOptions[4].options = res.data.map(item => {
        return {
          value: item["dictValue"],
          label: item["dictLabel"]
        };
      });
    });
    getDicts("clr_pub_type").then(res => {
      this.formOptions[5].options = res.data.map(item => {
        return {
          value: item["dictValue"],
          label: item["dictLabel"]
        };
      });
    });
    getDicts("clr_pub_website").then(res => {
      this.formOptions[7].options = res.data.map(item => {
        return {
          value: item["dictValue"],
          label: item["dictLabel"]
        };
      });
    });
  },
  mounted() {
    this.getList();
    this.department();
  },
  methods: {
     // 获取搜索表单提交的数据
     onSearch(val) {
      if(val.applyTime){
      this.dateRange = val.applyTime
        }
      this.queryParams = Object.assign(this.queryParams, val);
      this.queryParams.pageNum = 1;
      this.getList();
   },
   // 重置
   onReset(data) {
    data.params = {}
      this.dateRange = []
      this.queryParams.pageNum = 1;
      this.queryParams = Object.assign(this.queryParams, data);
     this.getList();
   },
    getList() {
      pubList(this.addDateRange(this.queryParams,this.dateRange )).then((response) => {
        console.log(response, "response");
        this.blackList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 修改
    handleEdit(row) {
      getPub(row.cappId).then((res) => {
        console.log(res, "详情");
        this.form = res.data;
        this.title = "修改";
        this.open = true;
      });
    },
    // 删除
    handleDelete(row) {
      this.$modal
        .confirm('是否确认删除"' + row.clrName + '"的论文申请的数据项？')
        .then(function () {
          return pubDel(row.cappId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          // this.form.clrExamineStaffList = this.clrExamineStaffList;
          if (this.form.cappId != null) {
            pubUpdata(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            applyPaperPub(this.form).then((response) => {
              this.$modal.msgSuccess("申请成功");
              this.open = false;
              this.getList();
            });
          }
          this.$refs.uploadList.uploadMethod();
        }
      });
    },
    cancel() {
      this.form = {};
      this.open = false;
    },
    // 获取部门信息
    async department() {
      await collegeSelect().then((res) => {
        this.deptOptions = res.data;
        this.formOptions[0].options = res.data.map(item => {
        return {
          value: item["deptId"],
          label: item["deptName"]
        };
      });
      });
    },
    // 上传
    changeLoud(data) {
      console.log(data,'数据')
      this.form.attachment = data
    },
    // 下载附件
    // handleExport(row) {
    //     if (row.attachment) {
    //       downloadGet(
    //         "/common/download",
    //         {
    //           params:{
    //             fileName: row.attachment
    //           }
    //         },
    //       );        } else {
    //       this.$modal.msgWarning("未找到上传结果");
    //     }
    //    },
    handleExport(row) {

let link = document.createElement("a"); 
//创建一个a标签
link.style.display = "none"; 
//将a标签隐藏
link.href = process.env.VUE_APP_BASE_API + row.attachment;
//给a标签添加下载链接  "域名+接口"  safe是一个动态的域名  后面的接口替换成你自己的下载接口
link.setAttribute("download", row.clrName + '论文发表'); 
// 此处注意，要给a标签添加一个download属性，属性值就是文件名称 否则下载出来的文件是没有属性的，空白白
document.body.appendChild(link);
//将上面创建的a标签加入到body的尾部
link.click(); 
//执行a标签


},
    // 申请
    handleAdd() {
      this.open = true;
      this.title = "申请";
      
    },
  },
};
</script>